package com.faf.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.faf.bean.SalesDetails;
import com.faf.dao.SalesDetailsDao;

public class SalesDetailsDaoImpl extends BaseDaoImpl implements SalesDetailsDao
{

	@Override
	public List<SalesDetails> getSalesDetailsList(int storeId,String year) throws SQLException
	{
		String sql = "select saleId,storeId,turnover,pv,month,year,day,saleRemark "
				+ "from salesdetails where storeId = ? and year = ?";
		executeQuery(sql,storeId,year);
		List<SalesDetails> salesDetailsList = new ArrayList();
		while(rs.next()){
			SalesDetails salesDetails = new SalesDetails(rs.getInt("saleId"), rs.getInt("storeId"), rs.getDouble("turnover"), 
					rs.getInt("pv"), rs.getString("month"), rs.getString("year"), rs.getString("day"), rs.getString("saleRemark"));
			salesDetailsList.add(salesDetails);
		}
		close();
		super.con = null;
		return salesDetailsList;
	}

	@Override
	public int judgeYearAndMonth(String month, String year) throws SQLException
	{
		String sql = "select saleId,storeId,turnover,pv,month,year,day,saleRemark from salesdetails where month = ? and year=?";
		executeQuery(sql,month,year);
		if(rs.next()) {
			return 1;
		}
		return 0;
	}

	@Override
	public int insertOrAlterTurnover(int storeId, double turnover, String month, String year, String way) throws SQLException
	{
		//way:1:修改,2:插入
		//update 表名 set 字段名 = 值 where 条件
		// insert into 表名（字段名1，字段名2） values (值1，值2) 
		if(way == "1") {
			String sql = "update salesdetails set turnover = ? where month=? and year=?";
			return executeUpdate(sql,turnover,month,year);
		}else if(way == "2") {
			String sql = "insert into salesdetails(storeId,turnover,month,year) values (?,?,?,?)";
			return executeUpdate(sql,storeId,turnover,month,year);
		}
		
		return 0;
	}

	@Override
	public int insertOrAlterPV(int storeId, long pv, String month, String year, String way) throws SQLException
	{
		if(way == "1") {
			String sql = "update salesdetails set pv = ? where month=? and year=?";
			return executeUpdate(sql,pv,month,year);
		}else if(way == "2") {
			String sql = "insert into salesdetails(storeId,pv,month,year) values (?,?,?,?)";
			return executeUpdate(sql,storeId,pv,month,year);
		}
		
		return 0;
	}
	

}
